package org.ssssssss.magicapi.redis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.ssssssss.magicapi.core.config.MagicAPIProperties;
import org.ssssssss.magicapi.core.config.MagicPluginConfiguration;
import org.ssssssss.magicapi.core.config.Resource;
import org.ssssssss.magicapi.core.model.Plugin;
import org.ssssssss.magicapi.core.service.MagicAPIService;
import org.ssssssss.magicapi.redis.RedisReceiver;

@Configuration
public class MagicRedisConfiguration implements MagicPluginConfiguration {


	@Autowired
	private MagicAPIService service;
	
	@Value("${spring.redis.msg-handler-api:/network/redis/msgHandler}")
	public String msgHandlerApi = "";
	
	@Value("${spring.redis.topics:CMS_RES}")
	public String topics = "";
	
	private final MagicAPIProperties properties;

	public MagicRedisConfiguration(MagicAPIProperties properties) {
		this.properties = properties;
	}

	/**
	 * 使用Redis存储
	 */
	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty(prefix = "magic-api", name = "resource.type", havingValue = "redis")
	public org.ssssssss.magicapi.core.resource.Resource magicRedisResource(RedisConnectionFactory connectionFactory) {
		Resource resource = properties.getResource();
		return new RedisResource(new StringRedisTemplate(connectionFactory), resource.getPrefix(), resource.isReadonly());
	}

	/**
	 * 注入redis模块
	 */
	@Bean
	public RedisModule redisFunctions(RedisConnectionFactory connectionFactory) {
		return new RedisModule(connectionFactory);
	}

	@Override
	public Plugin plugin() {
		System.out.println("redis插件:msgHandlerApi="+msgHandlerApi);
		if(service!=null) {
			Const.setService(service);
			Const.setMsgHandlerApi(msgHandlerApi);
		}
		else {
			System.out.println("Redis服务  @Autowired MagicAPIService ERROR");
		}
		return new Plugin("Redis");
	}
	
	// zcg
    @Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty(prefix = "magic-api", name = "resource.type", havingValue = "redis")
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory,
    		MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        String[] topicList = topics.split(",");
        List<PatternTopic> pTopics = new ArrayList<>();
        for (String channel : topicList) {
            pTopics.add(new PatternTopic(channel.trim()));
        }
        container.addMessageListener(listenerAdapter, pTopics);
        return container;
    }
//    
    @Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty(prefix = "magic-api", name = "resource.type", havingValue = "redis")
    public MessageListenerAdapter listenerAdapter() {
        MessageListenerAdapter adapter = new MessageListenerAdapter(new RedisReceiver(),"handleMessage");
        return adapter;
    }
    

}
